﻿using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

[assembly: WebResource("Hxsoft.Web.UI.WebControls.DateTimeValidator.DateTimeValidator.js", "text/javascript", PerformSubstitution = true)]

namespace Hxsoft.Web.UI.WebControls
{
	/// <summary>
	/// 日期验证自定义控件。
	/// </summary>
	[ToolboxData("<{0}:DateTimeValidator runat=server></{0}:DateTimeValidator>")]
	public class DateTimeValidator : BaseValidator
	{
		#region Override (重写)
		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			base.AddAttributesToRender(writer);

			if (RenderUplevel)
			{
				writer.AddAttribute("evaluationfunction", "DateTimeValidatorEvaluateIsValid", false);
			}
		}

		protected override bool EvaluateIsValid()
		{
			// 获取控件值
			string controlValue = GetControlValidationValue(ControlToValidate);
			if (string.IsNullOrEmpty(controlValue))
				return true;

			DateTime temp = new DateTime();
			return DateTime.TryParse(controlValue, out temp);
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

			if (RenderUplevel)
			{
				Type type = this.GetType();
				if (!Page.ClientScript.IsClientScriptIncludeRegistered(type, "DateTimeValidator.js"))
				{
					string url = Page.ClientScript.GetWebResourceUrl(type, "Hxsoft.Web.UI.WebControls.DateTimeValidator.DateTimeValidator.js");
					Page.ClientScript.RegisterClientScriptInclude(type, "DateTimeValidator.js", url);
				}
			}
		}
		#endregion
	}
}